var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data: {
    id: 0, //团购ID
    showType: 0,//0中1成功2失败
    ruleId: 0,
    groupon: {},
    linkGrouponId: 0,
    joiners: [],
    orderInfo: {},
    goodsId: 0, //获取第一个商品ID
    orderGoods: []
  },

  onLoad: function (options) {
    console.log('ruleId:' + options.ruleId);
    this.setData({
      id: options.id
    });
    this.setData({
      ruleId: options.ruleId
    });
    this.setData({
      showType: options.type
    });
    this.getOrderDetail();
  },

  //再次发起拼团
  jumpPages: function (event) {
    var goodsId = event.currentTarget.dataset.id;
    var ruleId = event.currentTarget.dataset.ruleid;
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + goodsId + '&ruleId=' + ruleId,
    })
  },

  // 页面分享
  onShareAppMessage: function () {
    let that = this;
    return {
      title: '邀请您一起购好物',
      path: '/pages/goods/goods?id=' + that.data.goodsId + '&ruleId=' + that.data.ruleId
    }
  },

  shareGroupon: function () {
    let that = this;
    wx.showActionSheet({
      itemList: ['分享给朋友', '分享到朋友圈'],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx.showModal({
            title: '提示',
            content: '点击右上角 "..." 转发给朋友',
            showCancel: false
          });
        } else if (res.tapIndex == 1) {
          that.saveShare();
        } else {
          console.log(res.tapIndex);
        }
      },
      fail: function (res) {
        console.log(res.errMsg);
      }
    })
  },

  // 保存分享图
  saveShare: function () {
    let that = this;
    wx.downloadFile({
      url: that.data.groupon.shareUrl,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            wx.showModal({
              title: '存图成功',
              content: '图片成功保存到相册了，可以分享到朋友圈了',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#a78845',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                }
              }
            })
          },
          fail: function (res) {
            console.log('fail')
          }
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getOrderDetail();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  getOrderDetail: function () {
    let that = this;
    util.request(api.GroupOnDetail, {
      grouponId: that.data.id
    }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          joiners: res.data.joiners,
          groupon: res.data.groupon,
          linkGrouponId: res.data.linkGrouponId,
          orderInfo: res.data.orderInfo,
          goodsId: res.data.orderGoods.goodsId,
          orderGoods: res.data.orderGoods
        });
      }
    });
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {}
});